package com.xtaller.lazy.lib.generator;

import com.xtaller.lazy.lib.convert.S;
import lombok.var;

import java.sql.*;

/**   
 * @author: Taller   
 * @date:   Mar 12, 2019 2:44:14 PM   
 * @version V1.0 
 * @Description: 数据库操作
 */
public class Conn {
	private String url, user, password, db, table;
	
	public Conn(String db, String table) {
		this.url = "jdbc:mysql://39.108.1.47/tpv3?characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
		this.user = "root";
		this.password = "Taller20180303!";
		this.db = db;
		this.table = table;
	}
	// 读取表注释
	public ResultSet tableInfo() {
		var sql = S.apppend("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_NAME='",
				table ,"' AND table_schema='",
				db ,"'");
		return run(sql);
	}
	// 读取表结构
	public ResultSet tableColumn() {
		var sql = S.apppend("SELECT COLUMN_Name,data_type,COLUMN_COMMENT from information_schema.columns WHERE TABLE_NAME='",
				table ,"' AND table_schema='",
				db ,"'");
		return run(sql);
	}
	/**
	 * 获取连接
	 * */
	private Connection get() {
		try {
			return DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	// 数据库查询
	private ResultSet run(String sql) {
		var conn = get();
		ResultSet rs;
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rs = null;
		}
        return rs;
	}

	
	
}
